package com.task.src.controller;


import com.github.pagehelper.PageInfo;
import com.task.src.dto.MyTask;
import com.task.src.dto.Task;
import com.task.src.dto.TaskDto;
import com.task.src.mapper.TaskMapper;
import com.task.src.service.TaskService;
import com.task.src.util.ResponseResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/task")
public class TaskController {
    @Resource
    private TaskService taskService;

    @Resource
    private HttpSession session;

    @Resource
    private TaskMapper taskMapper;
    @RequestMapping("/all")
    public PageInfo<List<Task>> selectAllTask(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                              @RequestParam(value = "pageSize",required = false,defaultValue = "4")int pageSize){
        int uid= (int) session.getAttribute("userid");
        return taskService.selectAllTask(pageNum, pageSize,uid);
    }

    @RequestMapping("/classify")
    public ResponseResult selectTaskDto(@RequestParam(value = "name",required = false,defaultValue = "顺路") String name){
        ResponseResult res=new ResponseResult();
        int id=(int)session.getAttribute("userid");
        String uid=id+"";
        if("undefined".equals(name)){
            String s="顺路";
            List<Task> list=taskService.selectTaskDto(s,uid);
            res.setData(list);
            return res;
        }
        List<Task> list=taskService.selectTaskDto(name,uid);
        res.setData(list);
        return res;
    }

    @RequestMapping("/param")
    public ResponseResult selectParamTask(int id){
        ResponseResult res=new ResponseResult();
        List<Task> list=taskMapper.selectParamTask(id);
        res.setData(list);
        return res;
    }

    @RequestMapping("/search")
    public ResponseResult queryTask(String msg){
        ResponseResult res=new ResponseResult();
        List<Task> list=taskMapper.selectTaskByQuery(msg);
        res.setData(list);
        return res;
    }

    @RequestMapping("/addTask")
    public String addPubTask(Task task){
        int uid=(int)session.getAttribute("userid");
        taskMapper.insertPubTask(task);
        int task_id=task.getId();
        taskMapper.insertToPubTable(uid,task_id);
        return "发布任务成功！";
    }
}
